home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / sweep / sweep.c < prev    next >
C/C++ Source or Header  |  1993-10-28  |  4KB  |  174 lines

  1. #include <math.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "ooglutil.h"
  5. #include "hpoint3.h"
  6. #include "point3.h"
  7. #include "vectP.h"
  8. #include "transform.h"
  9. #include "transform3.h"
  10. #include "sweep.h"
  11.  
  12. static char msg[] = "sweep.c";
  13.  
  14. Geom *TranslationSweep(float length, Point3 *p, Geom *g) {
  15.   int h, i, j, k;
  16.   Vect *v;
  17.   Geom *new;
  18.   int nverts;
  19.   int npoly;
  20.   HPoint3 hp3, *point4;
  21.   int *nvert, *vert;
  22.  
  23.   if (g == NULL) return NULL;
  24.   if (strcmp(GeomName(g), "vect")) {
  25.     OOGLError(1, "TranslationSweep called with non-vector object.");
  26.     return NULL;
  27.   }
  28.  
  29.   length /= Pt3Length(p);
  30.   hp3.x = p->x * length;
  31.   hp3.y = p->y * length;
  32.   hp3.z = p->z * length;
  33.   hp3.w = 1;
  34.   v = (Vect *) g;
  35.   nverts = abs(v->nvert);
  36.  
  37.   point4 = OOGLNewNE(HPoint3, nverts * 2, msg);
  38.   for (i = 0; i < nverts; i++) {
  39.     HPt3Copy(&v->p[i], &point4[i]);
  40.     HPt3Copy(&v->p[i], &point4[i + nverts]);
  41.     HPt3Add(&point4[i + nverts], &hp3, &point4[i + nverts]);
  42.   }
  43.  
  44.   nvert = OOGLNewNE(int, nverts, msg);
  45.   vert = OOGLNewNE(int, nverts * 4, msg);
  46.   for (i = 0; i < nverts; i++) nvert[i] = 4;
  47.   for (h = i = k = 0; i < v->nvec; i++) {
  48.     for (j = 0; j < abs(v->vnvert[i]);) {
  49.       vert[h++] = k + j;
  50.       vert[h++] = k + nverts + j++;
  51.       vert[h++] = k + nverts + j;
  52.       vert[h++] = k + j;
  53.     }
  54.     if (j) h -= 4;
  55.     if (v->vnvert[i] < 0) {
  56.       vert[h++] = k;
  57.       vert[h++] = k + nverts;
  58.       vert[h++] = k + nverts + j - 1;
  59.       vert[h++] = k + j - 1;
  60.     } 
  61.     k += j;
  62.   }
  63.  
  64.   npoly = h / 4;
  65.  
  66.   new = GeomCreate("polylist",
  67.            CR_NPOLY, npoly,
  68.            CR_POINT4, point4,
  69.            CR_NVERT, nvert,
  70.            CR_VERT, vert,
  71.            CR_END);
  72.  
  73.   OOGLFree(point4);
  74.   OOGLFree(nvert);
  75.   OOGLFree(vert);
  76.  
  77.   return(new);
  78.  
  79. }
  80.  
  81. Geom *RotationSweep(float angle, Point3 *end, Point3 *axis, 
  82.             int divisions, Geom *g) {
  83.   int h, i, j, k, m;
  84.   Vect *v;
  85.   Geom *new;
  86.   int v_nvert;
  87.   int npoly;
  88.   HPoint3 *point4;
  89.   int *nvert, *vert;
  90.   Transform T, TInv, R;
  91.   int fullcircle = 0;
  92.  
  93.   if (g == NULL) return NULL;
  94.   if (strcmp(GeomName(g), "vect")) {
  95.     OOGLError(1, "RotationSweep called with non-vector object.");
  96.     return NULL;
  97.   }
  98.  
  99.   /* Hack to deal with things that should go all the way around */
  100.   if (fabs(angle - 2.0 * M_PI) < .01) fullcircle = 1;
  101.  
  102.   v = (Vect *) g;
  103.  
  104.   v_nvert = abs(v->nvert);
  105.  
  106.   TmTranslate(T, end->x, end->y, end->z);
  107.   TmInvert(T, TInv);
  108.   TmRotate(R, angle / (float)divisions, axis); 
  109.  
  110.   point4 = OOGLNewNE(HPoint3, v_nvert * (divisions + 1), msg);
  111.   for (i = 0; i < v_nvert; i++) {
  112.     HPt3Copy(&v->p[i], &point4[i * (divisions+1)]);
  113.     for (j = 1; j < divisions+1; j++) {
  114.       HPt3Copy(&point4[i*(divisions+1) + j - 1], 
  115.            &point4[i*(divisions+1) + j]);
  116.       HPt3Transform(TInv, &point4[i*(divisions+1) + j], 
  117.             &point4[i*(divisions+1) + j]);
  118.       HPt3Transform(R, &point4[i*(divisions+1) + j], 
  119.             &point4[i*(divisions+1) + j]);
  120.       HPt3Transform(T, &point4[i*(divisions+1) + j],
  121.             &point4[i*(divisions+1) + j]);
  122.     }
  123.   }
  124.  
  125.   nvert = OOGLNewNE(int, v_nvert * (divisions+1), msg);
  126.   vert = OOGLNewNE(int, v_nvert * (divisions+1) * 4, msg);
  127.   for (i = 0; i < v_nvert * (divisions+1); i++) nvert[i] = 4;
  128.   for (h = i = k = 0; i < v->nvec; i++) {
  129.     for (j = 0; j < abs(v->vnvert[i]); j++) {
  130.       for (m = 1; m < divisions + 1; m++) {
  131.     vert[h++] = ((k + j)*(divisions+1)) + m - 1;
  132.     vert[h++] = ((k + j)*(divisions+1)) + m;
  133.     vert[h++] = ((k + j + 1)*(divisions+1)) + m;
  134.     vert[h++] = ((k + j + 1)*(divisions+1)) + m - 1;
  135.       }
  136.       if (fullcircle) {
  137.     vert[h-2] = (k + j + 1) * (divisions + 1);
  138.     vert[h-3] = (k + j) * (divisions + 1);
  139.       }
  140.     }
  141.     if (j) h -= 4 * divisions;
  142.     if (v->vnvert[i] < 0) { 
  143.       for (m = 1; m < divisions + 1; m++) {
  144.     vert[h++] = (k) * (divisions + 1) + m - 1;
  145.     vert[h++] = (k) * (divisions + 1) + m;
  146.     vert[h++] = (k + j - 1) * (divisions + 1) + m;
  147.     vert[h++] = (k + j - 1) * (divisions + 1) + m - 1;
  148.       }
  149.       if (fullcircle) {
  150.     vert[h-2] = (k + j - 1) * (divisions + 1);
  151.     vert[h-3] = k * (divisions + 1); 
  152.       }
  153.     }
  154.     k += j;
  155.   }
  156.   
  157.   npoly = h / 4;
  158.  
  159.   new = GeomCreate("polylist",
  160.            CR_NPOLY, npoly,
  161.            CR_POINT4, point4,
  162.            CR_NVERT, nvert,
  163.            CR_VERT, vert,
  164.            CR_END);
  165.  
  166.   OOGLFree(point4);
  167.   OOGLFree(nvert);
  168.   OOGLFree(vert);
  169.  
  170.   return(new);
  171.  
  172. }
  173.  
  174.